<?php


namespace app\admin\controller;


use app\common\controller\admin\Base;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

class Sysconfig extends Base
{
    protected $noNeedLogin = [];

    protected $model;


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\SysConfig;
    }

    public function index()
    {
        //设置过滤方法
        if ($this->request->isPost()) {
            $sort = 'id';
            $order = 'desc';
            $list = $this->model
                ->field('*')
                ->order($sort, $order)
                ->select();
            $data = [
                'list' => $list,
            ];
            $this->success('列表', '', $data);
        }
    }

    /**
     * 禁止新增
     */
    public function add()
    {
        $this->error('添加失败！该模块不允许添加');
    }

    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error('不存在的数据', '', [], 404);
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("");
            if ($params) {
                $params = $this->preExcludeFields($params);
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = $name . '.edit';
                        $row->validateFailException(true)->validate($validate);
                    }
                    $params['admin_id'] = $this->admin['id'];
                    $params['according'] = json_encode($params['according']);
                    $result = $row->allowField(true)->isUpdate(true)->save($params);
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success('修改成功');
                } else {
                    $this->error('修改失败');
                }
            }
            $this->error('参数不能为空！');
        }
    }

    /**
     * 获取详情
     * @param string $ids
     */
    public function details($ids = "")
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error('未查询到数据！', '', [], 404);
        }

        $data = json_decode($row['according'], true);
        $this->success('详情数据', '', $data);
    }

    /**
     * 禁止删除
     */
    public function del($ids = "")
    {
//        parent::del($ids);
        $this->error('删除失败！该模块不允许删除');
    }


}